package com.bjtime.hprinter.Fragments.attrs

import android.content.Intent
import android.os.Bundle
import android.support.v4.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.CheckBox
import android.widget.RadioButton
import android.widget.TextView
import com.bjtime.designer.widget.BaseLabelView
import com.bjtime.designer.widget.ImageView2
import com.bjtime.hprinter.Dialogs.MIUIInputDialog
import com.bjtime.hprinter.R
import com.bjtime.hprinter.utils
import java.io.File


class image : Fragment, View.OnClickListener {

    constructor()

    companion object {
        fun newInstance () : image {
            return image ()
        }
    }

    private var mView : View? = null
    private var BRView : ImageView2? = null
    private var SetImgBtn : TextView? = null

    private var mFixCheckBtn : RadioButton? = null;
    private var mFixConterBtn : RadioButton? = null;


    fun setView (v : BaseLabelView) {
        BRView = v as ImageView2
        when (BRView!!.getType ()) {
            1->  {
                mFixCheckBtn!!.isChecked = true
                mFixConterBtn!!.isChecked = false
            }
            0-> {
                mFixConterBtn!!.isChecked = true
                mFixCheckBtn!!.isChecked = false
            }
        }

    }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        mView = LayoutInflater.from(context).inflate(R.layout.viewpager_attrs_layout_image, null)

        SetImgBtn = mView!!.findViewById(R.id.img_tv_select)
        SetImgBtn!!.setOnClickListener(this)

        mFixCheckBtn = mView!!.findViewById(R.id.img_type_stre)
        mFixConterBtn = mView!!.findViewById(R.id.img_type_tile)

        mFixCheckBtn!!.setOnCheckedChangeListener({v,b->
            if (b)
                if (BRView != null)
                    BRView!!.setType(1)
        })

        mFixConterBtn!!.setOnCheckedChangeListener({v,b->
            if (b)
                if (BRView != null)
                    BRView!!.setType(0)
        })

        return mView
    }


    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode == 6666) {
            if (data!!.data != null) {
                var path = utils.getPath(context!!, data!!.data)
                if (BRView != null) {
                    var file = File (path)
                    BRView!!.setImage (file.readBytes())
                }

            }


        }
    }

    override fun onClick(v: View?) {
        if (v == SetImgBtn) {
            val intent = Intent(Intent.ACTION_GET_CONTENT)
            intent.type = "image/*"
            intent.addCategory(Intent.CATEGORY_OPENABLE)
            startActivityForResult(intent, 6666)
            return
        }
        /*
        var dialog  = MIUIInputDialog(context!!)
        dialog.setTitle("一维码内容")
        dialog.show()
        dialog.setMaxLenght(80)
        dialog.setString (QRText!!.text.toString())
        */
    }
}